

var total = 0;
var pageIndex = 0;
var pageSize = 0;
var totalRec = 0;
var pageInfo = new PageInfo();
var arg3s;
function initPage(total, pageIndex, pageSize, totalRec) {
	this.total = total;
	this.pageIndex = pageIndex;
	this.pageSize = pageSize;
	this.totalRec = totalRec;
	showPageSize();
	showPages();
}

function showPages() {
	//var i=((pageIndex-(pageIndex%5))/5)*5+1;
	var i = (Math.ceil(pageIndex / 5) - 1) * 5 + 1;
	$("#Flexigrid_Bar").html(""); 
	if (i == 1) { 
		$("#Flexigrid_Bar").append("<span>&lt;&lt;上5页</span>");
	} else {
		$("#Flexigrid_Bar").append(
				"<a id='upPage' href="
						+ "\"javascript:ajaxPage('back')\">&lt;&lt;上5页</a>");
	}
	$("#Flexigrid_Bar").append("&nbsp;");
	var n = 5;
	while (n--) {
		if (i == pageIndex) {
			$("#Flexigrid_Bar").append("<font color='red'>" + i + "</font>");
		} else {
			$("#Flexigrid_Bar").append(
					"<a class='indexPage' href='javascript:ajaxPageClick(" + i
							+ ")'>" + i + "</a>");
		}
		$("#Flexigrid_Bar").append("&nbsp;");
		if (i >= total)
			break; 
		i++;
	}

	if (((Math.ceil(pageIndex / 5)) * 5) < total) {
		$("#Flexigrid_Bar").append(
				"<a id='downPage' href="
						+ "\"javascript:ajaxPage('front')\">下5页&gt;&gt;</a>");
	} else {
		$("#Flexigrid_Bar").append("<span>下5页&gt;&gt;</span>");
	}
	$("#Flexigrid_Bar").append("&nbsp;");
}


function showPageSize() {
	var num = 10;
	$("#setupNum").html("");
	$("#setupNum").append("<span>每页显示数量:</span>");
	$("#setSize").width(50);
	$("#setSize").empty();
	for ( var i = 10; i <= 50; i += 20) {
		var opt = $("<option value='" + i + "'>" + i + "</option>");
		$("#setSize").append(opt);
	}
	setTimeout(function() {
				$("#setSize").val(pageSize)
			}, 0.1)

	
	$("#AllPage").html("");
	$("#AllPage").append("共" + total + "页/共" + totalRec + "条");

	$("#checkPage").html("");
	$("#checkPage")
			.append(
					"<input type='text' id='txtPage' name='txtPage' size='5'/><span>页</span><a id='goPage' href='javascript:ajaxGoPage();'>GO</a>");

}

function ajaxGoPage() {
	var page = $("#txtPage").attr("value");
	var checkPage = /^\d+$/;
	var pagetotal = total;
	if (page == null || page == "" || !checkPage.test(page)) {
		alert("请输入一个正整数!");
		return;
	}
	if (page > pagetotal) {
		alert("已经达到最大页数，无法继续查看到!");
		return;
	}
	if (page < 1) {
		alert("请输入一个大于0的整数!");
		return;
	}
	var index = page;
	var size = $("#setSize").val();
	pageInfo.pageIndex = index;
	pageInfo.pageSize = size;
	executeQuery();
	$("#setSize").empty();
}

function ajaxPageClick(i) {
	var index = i;
	var size = $("#setSize").val();
	//alert("index  " + index)
	//	alert("size  " + size)
	pageInfo.pageIndex = index;
	pageInfo.pageSize = size;
	executeQuery();
	$("#setSize").empty();
}

function ajaxChangeCount(pSize) {
	var index = 1;
	var size = pSize;
	pageSize = pSize;
	pageInfo.pageIndex = index;
	pageInfo.pageSize = size;
	executeQuery();
	$("#setSize").empty();
}

function ajaxPage(method) {
	if (method == "back") {
		pageIndex = (Math.ceil(pageIndex / 5) - 1) * 5;
	} else {
		pageIndex = (Math.ceil(pageIndex / 5)) * 5 + 1;
	}
	var index = pageIndex;
	var size = $("#setSize").val();
	pageInfo.pageIndex = index;
	pageInfo.pageSize = size;
	executeQuery();
	$("#setSize").empty();
}

function executeQuery(remoteFunction,remoteArgs) {

	if (arguments.length==0) {
		args = arg3s;
	} else {
		args=dealArgs(remoteFunction,remoteArgs);
		arg3s = args;

	}

	var arg0s = args.arguments;
	var arg1s = [];
	var i = 0
	for (i = 0; i < arg0s.length; i++) {
		arg1s.push(arg0s[i]);
	}
	arg1s.push(pageInfo);
	arg1s.push(args.callBackFunction);
	args.remoteFunction.apply(this, arg1s);

}

function dealArgs(remoteFunction,remoteArgs){
	var args = {};
	args.remoteFunction = remoteFunction;
	args.arguments = [];
	for(var i=0;i<remoteArgs.length;i++){
		
		if(i==remoteArgs.length-1&&typeof remoteArgs[i]=='function'){
			args.callBackFunction = remoteArgs[i];
		}else{
			args.arguments.push(remoteArgs[i]);
		}
	}
	return args;
}